package com.spring.shops.admin.controller.product;

import com.spring.shops.model.entity.TzCategory;
import com.spring.shops.model.result.CategoryResult;
import com.spring.shops.model.vo.CategoryVo;
import com.spring.shops.service.CategoryService;
import lombok.ToString;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@ToString
@RestController
@RequestMapping("/prod/category")
public class ProdCategoryController {

    @Resource
    public CategoryService categoryService;

    @ResponseBody
    @GetMapping("/table")
    public CategoryResult table(){
        TzCategory[] table1 = categoryService.tableSearch();
        return CategoryResult.tableSuccess(table1);
    }

    @ResponseBody
    @GetMapping("/listCategory")
    public CategoryResult list(){
        TzCategory[] table1 = categoryService.tableSearch();
        return CategoryResult.tableSuccess(table1);
    }

    @PostMapping
    public CategoryResult  save(@RequestBody CategoryVo vo){
        System.out.println(vo.toString());
        int i = categoryService.save(vo);
        if (i>0) {
            return CategoryResult.saveSuccess();
        }else return CategoryResult.saveFail();
    }
    @DeleteMapping("{id}")
    public CategoryResult Delete(@PathVariable("id") long id){
        boolean delete = categoryService.delete(id);
        if (delete==false){
            return CategoryResult.deleteFail();
        }else {
            return  CategoryResult.deleteSuccess();
        }
    }

    @GetMapping("info/{id}")
    public CategoryResult info(@PathVariable("id") long id){
        TzCategory info = categoryService.info(id);
        return CategoryResult.infoSuccess(info);
    }

    @PutMapping
    public CategoryResult update(@RequestBody TzCategory tzCategory){
        int update = categoryService.update(tzCategory);
        if (update>0){
            return CategoryResult.updateSuccess();
        }else
            return CategoryResult.updateFail();
    }
}
